package com.neugh.config;

import com.neugh.config.realm.UserRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



import java.util.HashMap;
import java.util.Map;


import org.apache.shiro.mgt.SecurityManager;

@Configuration
public class ShiroConfig {

    //核心过滤器
    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
        //配置拦截url
        //不拦截anon 拦截 授权authc
        Map<String, String> filterChainDefinitionMap=new HashMap<String, String>();
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/img/**", "anon");
        filterChainDefinitionMap.put("/lib/**", "anon");

        //登录处理接口不拦截
        filterChainDefinitionMap.put("/Neugh/itcy/login", "anon");
        filterChainDefinitionMap.put("/**", "anon");

        factoryBean.setLoginUrl("/");
        factoryBean.setSuccessUrl("/myinfo");
        factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        factoryBean.setSecurityManager(securityManager);

        return factoryBean;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm((Realm) userRealm());
        return securityManager;
    }

    @Bean
    public UserRealm userRealm(){
        UserRealm userReaml=new UserRealm();
        return userReaml;
    }
}
